/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TipoUvaView.java
 *
 * Created on 02/11/2011, 17:35:47
 */
package praticav.view;

import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.exception.ConstraintViolationException;
import praticav.dao.TipoUvaDAO;
import praticav.model.TipoUva;

/**
 *
 * @author Katiane
 */
public class TipoUvaView extends javax.swing.JFrame {
    int cod;
    String desc;
    int conta;

    private static void EXIT_ON_CLOSE() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /** Creates new form TipoUvaView */
    public TipoUvaView() {
        conta=0;        
        initComponents();
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtDescricaoUva = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();

        setTitle("Cadastro de Tipos de Uva");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Descrição da Uva"));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtDescricaoUva, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(txtDescricaoUva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/praticav/images/close.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/praticav/images/save.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 191, Short.MAX_VALUE)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnSalvar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
          
    
    public TipoUvaView(int codigo, String descricao) {
        try {
            cod = codigo;
            desc = descricao;
            conta=1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();        
        txtDescricaoUva.setText(""+descricao);
    }
    
    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        // Salvar
      if (txtDescricaoUva.getText().equals(""))
      {
          JOptionPane.showMessageDialog(null, "O campo DESCRIÇÃO deve estar preenchido!", "Atenção", WIDTH);
      } else
      {
          if (conta==0)
          {
            TipoUva f = new TipoUva();
            f.setUvadescricao(txtDescricaoUva.getText());
            TipoUvaDAO dao = new TipoUvaDAO();

            try{
                dao.inserir(f);
            } catch(ConstraintViolationException ce){
                JOptionPane.showMessageDialog(null, "Erro ao gravar os dados!!!  "+ ce.getMessage());
            } catch (HibernateException ex){
                JOptionPane.showMessageDialog(null, "Erro: "+ex.getMessage());
            }

            txtDescricaoUva.setText("");
            this.hide();
          } 
          else
          {
            TipoUva f = new TipoUva();
            f.setUvacodigo(cod);
            f.setUvadescricao(txtDescricaoUva.getText());
            TipoUvaDAO dao = new TipoUvaDAO();

            try{
                dao.editar(f);
            } catch(ConstraintViolationException ce){
                JOptionPane.showMessageDialog(null, "Erro ao modificar os dados!!!  "+ ce.getMessage());
            } catch (HibernateException ex){
                JOptionPane.showMessageDialog(null, "Erro: "+ex.getMessage());
            }

            txtDescricaoUva.setText("");
            this.hide();
          }
      }  
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // Cancelar
      this.hide();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        //Adicionar titulo na form
        if (conta == 0)
        {
            this.setTitle("Cadastro de Tipos de Uva");
        }else
        {
            this.setTitle("Alteração de Tipos de Uva");
        }
    }//GEN-LAST:event_formWindowOpened

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtDescricaoUva;
    // End of variables declaration//GEN-END:variables

}
